package com.pktechan.manage.utils;

import com.pktechan.manage.entity.CustBase;
import com.pktechan.manage.entity.SysOperate;
import com.pktechan.manage.entity.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <br>
 * <b>功能：</b>SessionUtils<br>
 * <b>作者：</b>Wang Jiahao<br>
 * <b>日期：</b> Nov 9, 2014 <br>
 * <b>版权所有：<b>版权所有(C) 2014，www.hoperun.com<br>
 */
public final class SessionUtils {

    protected static final Logger logger = LoggerFactory.getLogger(SessionUtils.class);

    private static final String SESSION_CUST_BASE = "custBase";
    private static final String SESSION_USER = "session_user";
    private static final String SESSION_MODULARMAP = "session_modular_map";
    private static final String SESSION_CURRENT_OPERATE_STRING = "session_current_operate_string";

    /**
     * 设置客户登录信息 到session
     *
     * @param request
     * @param custBase
     */
    public static void setCustBase(HttpServletRequest request, CustBase custBase) {
        request.getSession(true).setAttribute(SESSION_CUST_BASE, custBase);
    }

    /**
     * 从session中获取客户登录信息
     *
     * @param request
     * @return SysUser
     */
    public static CustBase getCustLoginAccount(HttpServletRequest request) {
        CustBase custBase = (CustBase) request.getSession(true).getAttribute(SESSION_CUST_BASE);
        if (custBase != null)
            return custBase;
        else
            return null;
    }

    /**
     * set a private constructor, to hide the implicit public one
     */
    public SessionUtils() {

    }

    /**
     * 设置session的值
     *
     * @param request
     * @param key
     * @param value
     */
    public static void setAttr(HttpServletRequest request, String key, Object value) {
        request.getSession(true).setAttribute(key, value);
    }

    /**
     * 获取session的值
     *
     * @param request
     * @param key
     */
    public static Object getAttr(HttpServletRequest request, String key) {
        return request.getSession(true).getAttribute(key);
    }

    /**
     * 删除Session值
     *
     * @param request
     * @param key
     */
    public static void removeAttr(HttpServletRequest request, String key) {
        request.getSession(true).removeAttribute(key);
    }


    /**
     * 设置用户信息 到session
     *
     * @param request
     * @param user
     */
    public static void setUser(HttpServletRequest request, SysUser user) {
        request.getSession(true).setAttribute(SESSION_USER, user);
    }

    /**
     * 设置拥有菜单权限列表 到session
     *
     * @param request
     * @param modularMap
     */
    public static void setModularMap(HttpServletRequest request, Map<String, SysOperate> modularMap) {
        request.getSession(true).setAttribute(SESSION_MODULARMAP, modularMap);
    }
    /**
     * 设置登录用户拥有权限的字符串 到session
     *
     * @param request
     * @param currentOperateString
     */
    public static void setCurrentOperateString(HttpServletRequest request, String currentOperateString) {
        request.getSession(true).setAttribute(SESSION_CURRENT_OPERATE_STRING, currentOperateString);
    }
    /**
     * 从session中获取登录用户拥有权限的字符串
     *
     * @param request
     * @return SysUser
     */
    public static String getCurrentOperateString(HttpServletRequest request) {
        return (String) request.getSession(true).getAttribute(SESSION_CURRENT_OPERATE_STRING);
    }
    /**
     * 从session中获取拥有菜单权限列表
     *
     * @param request
     * @return SysUser
     */
    public static Map<String, SysOperate> getModularMap(HttpServletRequest request) {
        return (Map<String, SysOperate>) request.getSession(true).getAttribute(SESSION_MODULARMAP);
    }
    /**
     * 从session中获取用户信息
     *
     * @param request
     * @return SysUser
     */
    public static void removeUser(HttpServletRequest request) {
        removeAttr(request, SESSION_USER);
    }

    /**
     * 从session中获取用户信息
     *
     * @param request
     * @return SysUser
     */
    public static SysUser getUser(HttpServletRequest request) {
        SysUser userInSession = (SysUser) request.getSession(true).getAttribute(SESSION_USER);
        if (userInSession != null)
            return (SysUser) userInSession.clone();
        else
            return null;
    }
}